package com.henu;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.henu.zmb.AddGoods_Activity;
import com.henu.zmb.AddLoseGoods_Activity;
import com.henu.zmb.Goods_MainActivity;
import com.henu.zmb.LoseGoods_MainActivity;
import com.henu.zmb.UserGoods_MainActivity;
import com.henu.zmb.UserLoseGoods_MainActivity;
import com.henu.zmb.entity.GoodsClass;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobBatch;
import cn.bmob.v3.BmobObject;
import cn.bmob.v3.datatype.BatchResult;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.QueryListListener;

public class Main2Activity extends AppCompatActivity {
    Button addgoods;
    Button addlosegoods;
    Button goodsmian;
    Button losegoodsmian;
    Button usergoods;
    Button userlosegoods;
    Button AddGoodsClass;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        //第一：默认初始化
        Bmob.initialize(this, "a24db0fac16b375a97072b85080ea20f");
        //批量添加

        AddGoodsClass = (Button)findViewById(R.id.AddGOODSCLASS) ;
        AddGoodsClass.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List<BmobObject> goodsclass = new ArrayList<BmobObject>();
                GoodsClass g0 = new GoodsClass(R.mipmap.bicycle,"自行车","普通自行车、山地车、折叠自行车");
                g0.setObjectId("7dd691166f");
                goodsclass.add(g0);
                GoodsClass g1 = new GoodsClass(R.mipmap.camera,"数码","shandiputong");
                g1.setObjectId("28fc8e5767");
                goodsclass.add(g1);
                GoodsClass g2 = new GoodsClass(R.mipmap.clothes,"衣物","shandiputong");
                g2.setObjectId("fa62a4baaf");
                goodsclass.add(g2);
                GoodsClass g3 = new GoodsClass(R.mipmap.computer,"电脑","shandiputong");
                g3.setObjectId("2bc8378da6");
                goodsclass.add(g3);
                GoodsClass g4 =new GoodsClass(R.mipmap.book,"书籍","shandiputong");
                g4.setObjectId("8bbe0509e9");
                goodsclass.add(g4);
                GoodsClass g5 =new GoodsClass(R.mipmap.motorcycle,"摩托车","shandiputong");
                g5.setObjectId("baac453ab6");
                goodsclass.add(g5);
                GoodsClass g6 =new GoodsClass(R.mipmap.household_appliances,"家电","shandiputong");
                g6.setObjectId("f781d8442c");
                goodsclass.add(g6);
                GoodsClass g7 =new GoodsClass(R.mipmap.furniture,"家具","shandiputong");
                g7.setObjectId("ba65396b2f");
                goodsclass.add(g7);
                GoodsClass g8 =new GoodsClass(R.mipmap.notebook_computer,"笔记本","shandiputong");
                g8.setObjectId("31d7bef15a");
                goodsclass.add(g8);
                GoodsClass g9 =new GoodsClass(R.mipmap.phone,"手机","shandiputong");
                g9.setObjectId("6729f61710");
                goodsclass.add(g9);
                GoodsClass g10 =new GoodsClass(R.mipmap.tricycle,"三轮车","shandiputong");
                g10.setObjectId("66f2819c53");
                goodsclass.add(g10);
                GoodsClass g11 =new GoodsClass(R.mipmap.electrombile,"电动车","shandiputong");
                g11.setObjectId("95e4a8d2e9");
                goodsclass.add(g11);
                new BmobBatch().updateBatch(goodsclass).doBatch(new QueryListListener<BatchResult>() {

                    @Override
                    public void done(List<BatchResult> o, BmobException e) {
                        if (e == null) {
                            for (int i = 0; i < o.size(); i++) {
                                BatchResult result = o.get(i);
                                BmobException ex = result.getError();
                                if (ex == null) {
                                    System.out.print("第" + i + "个数据批量添加成功：" + result.getCreatedAt() + "," + result.getObjectId() + "," + result.getUpdatedAt());
                                } else {
                                    System.out.print("第" + i + "个数据批量添加失败：" + ex.getMessage() + "," + ex.getErrorCode());
                                }
                            }
                        } else {

                        }
                    }
                });
            }
        });
        // 单个添加
        /*
        GoodsClass s = new GoodsClass(R.mipmap.bicycle,"自行车","普通自行车、山地车、折叠自行车");
        s.save(new SaveListener<String>() {
            @Override
            public void done(String objectId,BmobException e) {
                if(e == null){
                    System.out.print("添加数据成功，返回objectId为："+objectId);
                }else{
                    System.out.print("创建数据失败：" + e.getMessage());
                }
            }
        });
        */
        /*
         BmobQuery<LoseGoods> q=new BmobQuery<>();
        q.findObjects(new FindListener<LoseGoods>() {
            @Override
            public void done(List<LoseGoods> list, BmobException e) {
                System.out.println(list.size());
            }
        });*/
        /*
        DBhelper<LoseGoods> dbhelper= new DBhelper<LoseGoods>();
        LoseGoods lg = new LoseGoods(R.mipmap.stars,"stars",null,"heda","sdfsf");
        dbhelper.Add(lg);
        */
        /*
        BmobQuery<GoodsClass> query = new BmobQuery<GoodsClass>();
        query.getObject("95e4a8d2e9", new QueryListener<GoodsClass>() {

            @Override
            public void done(GoodsClass object, BmobException e) {
                if(e==null){
                    //获得playerName的信息
                    System.out.println("_______________________________________"+object.getGoodsClass_Name());
                }else{
                    System.out.println("bmob失败："+e.getMessage()+","+e.getErrorCode());
                }
            }

        });
*/
        /*
        BmobQuery<GoodsClass> query = new BmobQuery<GoodsClass>();
        //查询playerName叫“比目”的数据
        //query.addWhereEqualTo("playerName", "比目");
        //返回50条数据，如果不加上这条语句，默认返回10条数据
        query.setLimit(50);
        //执行查询方法
        query.findObjects(new FindListener<GoodsClass>() {
            @Override
            public void done(List<GoodsClass> object, BmobException e) {
                if(e==null){
                    System.out.println("查询成功：共"+object.size()+"条数据。");
                    for (GoodsClass gc : object) {
                        System.out.println("_______________________________________"+gc.getGoodsClass_Name());
                    }
                }else{
                    System.out.println("bmob失败："+e.getMessage()+","+e.getErrorCode());
                }
            }
        });*/
        addgoods = (Button)findViewById(R.id.addgoods);
        addlosegoods = (Button)findViewById(R.id.addlosegoods);
        losegoodsmian = (Button)findViewById(R.id.losegoodsmain);
        goodsmian = (Button)findViewById(R.id.goodsmain);
        usergoods = (Button)findViewById(R.id.usergoods);
        userlosegoods = (Button)findViewById(R.id.userlosegoods);
        goodsmian.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Main2Activity.this,Goods_MainActivity.class); // 设置跳转的路径
                startActivity(intent); // 跳转
            }
        });
        losegoodsmian.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Main2Activity.this,LoseGoods_MainActivity.class); // 设置跳转的路径
                startActivity(intent); // 跳转
            }
        });
        addgoods.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Main2Activity.this,AddGoods_Activity.class); // 设置跳转的路径
                startActivity(intent); // 跳转
            }
        });
        addlosegoods.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Main2Activity.this,AddLoseGoods_Activity.class); // 设置跳转的路径
                startActivity(intent); // 跳转
            }
        });
        usergoods.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Main2Activity.this,UserGoods_MainActivity.class); // 设置跳转的路径
                startActivity(intent); // 跳转
            }
        });
        userlosegoods.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Main2Activity.this,UserLoseGoods_MainActivity.class); // 设置跳转的路径
                startActivity(intent); // 跳转
            }
        });

    }

}
